package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.ScheduleMapper;
import com.link_with_health.model.ScheduleEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.ScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("scheduleService")
public class ScheduleServiceImpl extends BaseService implements ScheduleService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    ScheduleMapper scheduleMapper;

    /**
     * 通过日程id获取日程表信息
     * @param id 日程id
     * @return  日程信息
     * @throws BaseException 异常
     */
    @Override
    public ScheduleEntity getScheduleById(Long id) throws BaseException {
        ScheduleEntity entity = null;
        try {
            entity = scheduleMapper.queryScheduleById(id);
        } catch (Exception e) {
            logger.error("[query schedule failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("SCHEDULE.ID: "+id);

        return entity;
    }

    /**
     * 通过医生id 获取日程表信息
     * @param id 日程id
     * @return  日程信息
     * @throws BaseException 异常
     */
    @Override
    public List<ScheduleEntity> getScheduleByDoctor(Long id) throws BaseException {
        List<ScheduleEntity> scheduleList = null;
        try {
            scheduleList = scheduleMapper.queryScheduleByDoctor(id);
        } catch (Exception e) {
            logger.error("[query schedule failed]",e);
        }
        if (scheduleList == null || scheduleList.isEmpty())
            throw new NullQueryResultException("SCHEDULE.DR_ID: "+id);

        return scheduleList;
    }

    /**
     * 获取所有日程表信息
     * @return 日程信息
     * @throws BaseException 异常
     */
    @Override
    public List<ScheduleEntity> getAllSchedule() throws BaseException {
        List<ScheduleEntity> scheduleList = null;
        try {
            scheduleList = scheduleMapper.queryAllSchedule();
        } catch (Exception e) {
            logger.error("[query schedule failed]",e);
        }
        if (scheduleList == null || scheduleList.isEmpty())
            throw new NullQueryResultException("ALL SCHEDULE");

        return scheduleList;
    }

    /**
     * 增加日程信息
     * @param schedule 日程信息
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addSchedule(ScheduleEntity schedule) throws BaseException {
        int result = 0;
        try {
            result = scheduleMapper.insertSchedule(schedule);
        } catch (Exception e) {

            logger.error("[add schedule failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT SCHEDULE FAILED: "+schedule);

        return result;
    }

    /**
     * 修改日程信息
     * @param schedule 日程信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeSchedule(ScheduleEntity schedule) throws BaseException {
        int result = 0;
        try {
            result = scheduleMapper.updateSchedule(schedule);
        } catch (Exception e){
            logger.error("[change schedule failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE SCHEDULE: "+schedule);
        return result;
    }

    /**
     * 删除日程信息
     * @param id 日程id
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deleteSchedule(int id) throws BaseException {
        int result = 0;
        try {
            result = scheduleMapper.deleteSchedule(id);
        } catch (Exception e){
            logger.error("[delete schedule failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("DELETE SCHEDULE.ID: "+id);

        return result;
    }
}
